//
//  YSOauthLoginViewContainer.m
//  Yueshi
//
//  Created by Northwan on 15/9/25.
//  Copyright © 2015年 悦食中国. All rights reserved.
//

#import "YSOauthLoginViewContainer.h"
#import "WXApi.h"
#import <TencentOpenAPI/TencentOAuth.h>
#import "WeiboSDK.h"

#define OauthBtnInset 34

@interface YSOauthLoginViewContainer()
@property (assign, nonatomic)NSInteger currentHaveWay;
@end
@implementation YSOauthLoginViewContainer

-(instancetype)init{
    if(self =[super init]){
        [self setBackgroundColor:[UIColor clearColor]];
        UIImage *loginImg = [UIImage imageNamed:@"icon_qq"];
        [self setFrame:CGRectMake(0, 0, loginImg.size.width*3+OauthBtnInset*2, loginImg.size.height)];
        
        _qqLoginBtn  = [[UIButton alloc]init];
        [_qqLoginBtn setBackgroundImage:loginImg forState:UIControlStateNormal];
        [_qqLoginBtn setTag:5];
        [_qqLoginBtn setFrame:CGRectMake(0, 0, loginImg.size.width, loginImg.size.height)];
        [self addSubview:_qqLoginBtn];

        _wxLoginBtn = [[UIButton alloc]init];
        [_wxLoginBtn setBackgroundImage:IMG(@"icon_weixin") forState:UIControlStateNormal];
        [_wxLoginBtn setFrame:CGRectMake(0, 0, loginImg.size.width, loginImg.size.height)];
        [_wxLoginBtn setTag:6];
        [self addSubview:_wxLoginBtn];
        
        _wbLoginBtn = [[UIButton alloc]init];
        [_wbLoginBtn setBackgroundImage:IMG(@"icon_weibo") forState:UIControlStateNormal];
        [_wbLoginBtn setFrame:CGRectMake(0, 0, loginImg.size.width, loginImg.size.height)];
        [_wbLoginBtn setTag:7];
        [self addSubview:_wbLoginBtn];
    }
    return self;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    if(![TencentOAuth iphoneQQInstalled]){
        [_qqLoginBtn removeFromSuperview];
    }
    if(![WXApi isWXAppInstalled] &&![WXApi isWXAppSupportApi]){
        [_wxLoginBtn removeFromSuperview];
    }
    if(![WeiboSDK isWeiboAppInstalled]){
        [_wbLoginBtn removeFromSuperview];
    }
    
    NSArray *subviews = self.subviews;
    if(subviews.count == 1){
       UIButton *button = subviews[0];
       button.x = (self.width-button.width)/2;
    }else if (subviews.count == 2){
        UIButton *button = subviews[0];
        UIButton *button1 = subviews[1];
        CGFloat leftX = (self.width - button.width)*0.5 - OauthBtnInset;
        CGFloat rightX = (self.width - button1.width)*0.5 + OauthBtnInset;
        button.x = leftX;
        button1.x = rightX;
    }else if (subviews.count == 3){
        UIButton *button = subviews[0];
        UIButton *button1 = subviews[1];
        UIButton *button2 = subviews[2];
        button.x = 0;
        button1.x = CGRectGetMaxX(button.frame)+OauthBtnInset;
        button2.x = CGRectGetMaxX(button1.frame)+OauthBtnInset;
    }
}

@end
